package cn.sgjk.train.member.controller;


import cn.sgjk.train.common.context.LoginMemberContext;
import cn.sgjk.train.common.resp.CommonResp;
import cn.sgjk.train.common.resp.PageResp;
import cn.sgjk.train.member.req.PassengerQueryReq;
import cn.sgjk.train.member.req.PassengerSaveReq;
import cn.sgjk.train.member.resp.PassengerQueryResp;
import cn.sgjk.train.member.service.PassengerService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/passenger")
public class PassengerController {

    private static final Logger LOG = LoggerFactory.getLogger(PassengerController.class);
    @Autowired
    private PassengerService passengerService;



    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody PassengerSaveReq passengerReq) {
        passengerService.save(passengerReq);
        return new CommonResp<>();
    }



    //get请求信息存储在url，post存储在请求体上
    //返回参数不用Passenger数据库实体类，重新创建一个实体类进行响应，比较规范
    @GetMapping("/query-list")
    public CommonResp<PageResp<PassengerQueryResp>> query(@Valid PassengerQueryReq passengerQueryReq) {
        //控制台能看某个会员的乘客金，也能看所有人的乘客，控制台是没有本地线程id，service是通用的业务，所以放在用户接口上
        passengerQueryReq.setMemberId(LoginMemberContext.getId());
        PageResp<PassengerQueryResp> passengerQueryResps = passengerService.queryList(passengerQueryReq);
        return new CommonResp<>(passengerQueryResps);
    }

    @GetMapping("/query-mine")
    public CommonResp<List<PassengerQueryResp>> queryMine() {
        List<PassengerQueryResp> passengers = passengerService.queryMine();
        return new CommonResp<>(passengers);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        passengerService.delete(id);
        return new CommonResp<>();
    }
}

